package com.hzit.mmcb.core.controller.api;


import com.hzit.common.result.R;
import com.hzit.mmb.base.utils.JwtUtils;
import com.hzit.mmcb.core.hfb.RequestHelper;
import com.hzit.mmcb.core.pojo.entity.LendReturn;
import com.hzit.mmcb.core.service.LendReturnService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 还款记录表 前端控制器
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/lendReturn")
public class ApiLendReturnController {
    @Autowired
    private LendReturnService lendReturnService;

    /**
     * 根据标的id查询还款计划
     */

    @GetMapping("/list/{id}")
    @ApiOperation("根据标的id查询还款计划")
    public R list(@PathVariable Long id){
        List<LendReturn> list=lendReturnService.listByLendId(id);
        return R.ok().data("list",list);
    }

    /**
     * 还款
     * @param lendReturn
     * @param token
     * @return
     */
    @ApiOperation("还款")
    @PostMapping("/auth/commitReturn/{lendReturn}")
    public R commitReturn(@PathVariable Long lendReturn,@RequestHeader("token") String token){
        Long userId = JwtUtils.getUserId(token);
        String formStr = lendReturnService.commitReturn(lendReturn,userId);
        return R.ok().data("formStr",formStr);
    }

    /**
     * 还款回调
     */
    @PostMapping("/notify")
    @ApiOperation("充值异步回调接口")
    public String notify(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        //判断验签是否成功
        if (RequestHelper.isSignEquals(map)){
            //判断状态是否是回款成功
            if ("0001".equals(map.get("resultCode"))) {
                String s = lendReturnService.notifyReturn(map);
                return s;
            }
        }
        return "fail";
    }
}

